package com.bckj.fastboot.flow.configuration;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
@ConfigurationProperties(prefix = "flow.datasource")
public class FlowConfiguration {

    private String url;
    private final static String driverClassName = "com.mysql.cj.jdbc.Driver";
    private String username;
    private String password;

    @Bean
    FlowDataSource flowDataSource() {
        DataSourceProperties properties = new DataSourceProperties();
        properties.setUrl(url);
        properties.setUsername(username);
        properties.setPassword(password);
        properties.setDriverClassName(driverClassName);
        return new FlowDataSource(properties.initializeDataSourceBuilder().type(HikariDataSource.class).build());
    }

    @Bean
    JdbcTemplate flowJdbcTemplate(FlowDataSource flowDataSource) {
        return new JdbcTemplate(flowDataSource.dataSource());
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
